package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesStockReportWriteRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesStockReportSDO;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.dto.BatchCreateRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.dto.DeleteRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.dto.DeleteResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.dto.UpdateResponse;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.model.MonthSalesStockReport;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.service.MonthSalesStockReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2023/3/8 10:51
 */
@Component
public class MonthSalesStockReportWriteRepositoryImpl implements MonthSalesStockReportWriteRepository {

    Log log = Log.getLogger(MonthSalesStockReportWriteRepositoryImpl.class);

    @Resource
    MonthSalesStockReportService monthSalesStockReportService;

    @Override
    public String create(MonthSalesStockReportSDO sdo) {
        MonthSalesStockReport monthSalesStockReport = new MonthSalesStockReport();
        BeanUtils.copyProperties(sdo,monthSalesStockReport);
        Map m = (Map)monthSalesStockReportService.create(monthSalesStockReport);
        Map result = (Map)m.get("result");
        return (String)result.get("lastInsertId");
    }

    @Override
    public Integer update(MonthSalesStockReportSDO sdo) {
        MonthSalesStockReport monthSalesStockReport = new MonthSalesStockReport();
        BeanUtils.copyProperties(sdo,monthSalesStockReport);
        UpdateResponse response = monthSalesStockReportService.update(monthSalesStockReport);
        return response.getCount();
    }

    @Override
    public Integer batchDelete(List<Long> ids) {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.setIds(ids);
        DeleteResponse response = monthSalesStockReportService.delete(deleteRequest);
        return response.getCount();
    }

    @Override
    public void batchCreate(List<MonthSalesStockReportSDO> sdoList) {
        List<MonthSalesStockReport> reportList = new ArrayList<>();
        int batch = 0;
        for(MonthSalesStockReportSDO sdo:sdoList){
            batch++;
            MonthSalesStockReport monthSalesStockReport = new MonthSalesStockReport();
            BeanUtils.copyProperties(sdo,monthSalesStockReport);
            reportList.add(monthSalesStockReport);
            if (batch%500==0){
                BatchCreateRequest request = new BatchCreateRequest();
                request.setReportList(reportList);
                Object response = monthSalesStockReportService.batchCreate(request);
                log.info("monthSalesStockReportService_batchCreate_reportList_size={},response={}", reportList.size(), JSON.toJSONString(response));
                reportList.clear();
            }
        }
        if (reportList.size()>0){
            BatchCreateRequest request = new BatchCreateRequest();
            request.setReportList(reportList);
            Object response = monthSalesStockReportService.batchCreate(request);
            log.info("monthSalesStockReportService_batchCreate2_reportList2_size={},response2={}", reportList.size(), JSON.toJSONString(response));
        }
    }
}
